package com.ecommerce.dal.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.ecommerce.dal.dao.IClientDAO;
import com.ecommerce.dal.entity.Client;
import com.ecommerce.dal.entity.Company;
import com.ecommerce.dal.entity.Rank;
import com.ecommerce.dal.util.CustomHibernateDAOSupport;

/**
 * always refer to PK as CLIENT_ID in HQL clauses.
 * 
 */
@Repository("clientDAO")
public class ClientDAO extends CustomHibernateDAOSupport implements IClientDAO {

	@Override
	public void save(Client client) {
		getHibernateTemplate().save(client);

	}

	@Override
	public void update(Client client) {
		getHibernateTemplate().update(client);

	}

	@Override
	public void delete(Client client) {
		getHibernateTemplate().delete(client);

	}

	@Override
	public Client findByClientId(long clientId) {
		List list = getHibernateTemplate().find(
		// "from Client where CLIENT_ID=?", clientId);
				"from Client as client where CLIENT_ID=?", clientId);
		return (Client) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public Client findByClientName(String clientUserName) {
		List list = getHibernateTemplate()
		// .find("select client from Client as client ,  User user where user.userId = CLIENT_ID and user.userName = ?",
				.find("select client from Client as client inner join client.user user where user.userName = ?",
						clientUserName);
		return (Client) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public List<Client> findAllClients() {
		List list = getHibernateTemplate().find("from Client");
		return  ((list != null && list.size()>0) ? list : null) ;
	}

	public Company findClientCompanyById(long clientId) {
		List list = getHibernateTemplate()
				.find("select company from Client as client inner join client.company as company where CLIENT_ID = ?",
						clientId);
		return (Company) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	public Company findClientCompanyByUserName(String clientUserName) {
		List list = getHibernateTemplate()
				.find("select company from Client as client inner join client.company as company inner join client.user as user where user.userName = ?",
						clientUserName);
		return (Company) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public int findClientStateById(long clientId) {
		List list = getHibernateTemplate().find(
				"select client.state from Client client where CLIENT_ID =?",
				clientId);
		return (Integer) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public Rank findClientRankById(long clientId) {
		List list = getHibernateTemplate()
				.find("select rank from Client client inner join client.rank as rank where CLIENT_ID =?",
						clientId);
		return (Rank) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	public Rank findClientRankByUserName(String clientUserName) {
		List list = getHibernateTemplate()
				.find("select rank from Client client inner join client.rank as rank inner join client.user as user where user.userName =?",
						clientUserName);
		return (Rank) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public int findClientStateByUserName(String clientUserName) {
		List list = getHibernateTemplate()
				.find("select client.state from Client client inner join client.user as user where user.userName =?",
						clientUserName);
		return (Integer) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	// not tested yet!
	@Override
	public Client findByMembershipCard(long membershipcardId) {
		List list = getHibernateTemplate()
				.find("select client from Client client inner join client.membershipCard membershipCard where membershipCard.membershipCardId= ?",
						membershipcardId);
		return (Client) ((list != null && list.size()>0) ?  list.get(0) : null) ;
	}

	@Override
	public boolean checkIfCustomer(long clientId) {
		List list = getHibernateTemplate()
				.find("select count(customer) from Client client inner join client.customer customer where CUSTOMER_ID = ?",
						clientId);
		return !((Long) (list.get(0))).equals(Long.valueOf(0)) ? true : false;
	}

	@Override
	public boolean checkClientId(long clientId) {
		List list = getHibernateTemplate().find(
				"select count(client) from Client client where CLIENT_ID = ?",
				clientId);
		return !((Long) (list.get(0))).equals(Long.valueOf(0)) ? true : false;
	}

	@Override
	public List<Client> findSponsorClients() {
		List list = getHibernateTemplate().find(
				"from Client client where client.sponsor = true");
		return ((list != null && list.size()>0) ?  list : null) ;
	}

	@Override
	public List<Client> findVerifiedClients() {
		List list = getHibernateTemplate().find(
				"from Client client where client.verified = true");
		return ((list != null && list.size()>0) ?  list : null) ;
	}

	@Override
	public List<Client> findNonVerifiedClients() {
		List list = getHibernateTemplate().find(
				"from Client client where client.verified = false");
		return ((list != null && list.size()>0) ?  list : null) ;
	}

	@Override
	public boolean isVerified(long clientId) {
		List list = getHibernateTemplate().find(
				"select client.verified from Client client where CLIENT_ID = ?",
				clientId);
		return ((Boolean) (list.get(0))).equals(Boolean.TRUE) ? true : false;
	}

}
